local skel = fk.CreateSkill {
  name = "emo__tianshi",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__tianshi"] = "天师",
  [":emo__tianshi"] = "每当未横置的角色受到雷属性伤害时，防止此伤害，你摸伤害值张牌，然后将一张【闪电】置于其下家判定区。",
}

--[[
-- 废案 每轮开始时，将牌堆中一张【闪电】或【浮雷】置于一号位的判定区。
skel:addEffect(fk.RoundStart, {
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local to = table.find(player.room.alive_players, function (p)
        return p.seat == 1
      end)
      if not to then return end
      local cid = table.find(player.room.draw_pile, function(id)
        local c = Fk:getCardById(id, true)
        return (c.name == "lightning" or c.name == "floating_thunder") and not to:isProhibitedTarget(c)
      end)
      if cid then
        event:setCostData(self, {tos = {to}, cards = {cid} } )
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(event:getCostData(self).cards, Card.PlayerJudge, event:getCostData(self).tos[1], fk.ReasonPut, skel.name, nil, true, player)
  end,
})
--]]

skel:addEffect(fk.DamageInflicted, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.damageType == fk.ThunderDamage and target and not target.chained
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {target}})
    return true
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(data.damage, skel.name)
    data:preventDamage()
    local to = target:getNextAlive()
    if not to or to == target then return end
    local room = player.room
    -- 优先拿处理区正在判定的闪电
    local cid = table.find(room.processing_area, function (id)
      return Fk:getCardById(id, true).name == "lightning"
    end)
    if not cid then
      cid = room:getCardsFromPileByRule("lightning", 1, "allPiles")[1]
    end
    if cid and not to:isProhibitedTarget(Fk:getCardById(cid)) then
      room:moveCardTo(cid, Card.PlayerJudge, to, fk.ReasonPut, skel.name, nil, true, player)
    end
  end,
})

return skel
